var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    marker:[],
    longitude:"113.324520",
    latitude:"23.099994"
  },

  onLoad: function (options) {
    var that=this;
    var coupons_code = options.coupons_code;
    wx.getSystemInfo({
      success: function (res) {
       that.setData({
         windowHeight: res.windowHeight,
         coupons_code: coupons_code
       })
      }
    })
  },

  onShow: function () {
    var that = this;
    that.getLocation()
  },

  //获取定位
  getLocation:function(){
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        that.getShopList();
      },
      fail: function (fail) {
        if (fail.errMsg == "getLocation:fail auth deny") {
          that.showModal()
        }
      }
    })
  },
  // 重新授权
  reAuthorization: function () {
    var that = this
    wx.openSetting({
      success: function (res) {
        if (res.authSetting["scope.userLocation"]) {
          that.getLocation();
        } else {
          that.showModal();
        }
      }
    })
  },

  showModal: function () {
    var that = this;
    wx.showModal({
      title: '警告',
      content: '若不授权，则无法使用',
      confirmText: '授权',
      cancelText: '不授权',
      success: function (success) {
        if (success.confirm) {
          that.reAuthorization();
        } else {
          wx.navigateBack({
            delta: 1
          })
        }
      }
    })
  },

  locationCol:function(e){
    var index = e.currentTarget.dataset.index;
    console.log(index);
    var lat = this.data.dataList[index].lat;
    var lng = this.data.dataList[index].lng;
    this.setData({
      longitude:lng,
      latitude:lat
    })
  },

  //获取店铺列表
  getShopList:function(){
    var that = this;
    var params = new Object()
    params.sku_code = this.data.coupons_code;
    params.lng = this.data.longitude;
    params.lat = this.data.latitude;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'winecabinet/shopAddr',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var list = res.data.data.list;
          var markers = [];
          if(list.length>0){
          for (var i = 0; i < list.length; i++){
            var marker = {
              id: list[i].shop_id,
              latitude: list[i].lat,
              longitude: list[i].lng,
              iconPath: '../../images/ic_point.png',
              width: 21,
              height: 32
            }

            markers.push(marker)
          }
          that.setData({
            dataList:res.data.data.list,
            markers: markers,
            longitude: list[0].lng,
            latitude: list[0].lat
          })
        }
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  }
})